<?php

namespace Trawind\Events\Subscribers\Inventory\Quantity;

use Trawind\Events\Events\Inventory\Fulfillment\ClosePickEvent;
use Trawind\Events\Events\Inventory\Fulfillment\DeletePickEvent;
use Trawind\Events\Events\Inventory\Quantity\DeleteControlRecordEvent;
use Trawind\Events\Events\Inventory\Quantity\StoreControlRecordEvent;
use Trawind\Events\Events\Inventory\Quantity\StoreQuantityControlEvent;
use Trawind\Events\Events\Inventory\Quantity\UpdateControlRecordEvent;
use Trawind\Events\Events\Inventory\Receipt\DeletePutawayEvent;
use Trawind\Events\Events\Inventory\Receipt\DeleteRejectsPutawayEvent;
use Trawind\Events\Events\Inventory\Receipt\StorePutawayEvent;
use Trawind\Events\Events\Inventory\Receipt\StoreRejectsPutawayEvent;
use Trawind\Extension\Models\Deploy\BusinessType;

class QuantityControlSubscriber
{
    private $businessType = \Trawind\Basics\Enums\BusinessType::INVENTORY_QUALITY_CONTROL;

    public function storePutaway($event)
    {
        if ($this->businessType == $event->payload['business_type_id']) {
            $repository = BusinessType::getEloquentById($this->businessType);
            $repository->callback($event->payload, StorePutawayEvent::class);
        }
    }

    public function deletePutaway($event)
    {
        if ($this->businessType == $event->payload['business_type_id']) {
            $repository = BusinessType::getEloquentById($this->businessType);
            $repository->callback($event->payload, DeletePutawayEvent::class);
        }
    }

    public function StoreRejectsPutaway($event)
    {
        if ($this->businessType == $event->payload['business_type_id']) {
            $repository = BusinessType::getEloquentById($this->businessType);
            $repository->callback($event->payload, StoreRejectsPutawayEvent::class);
        }
    }

    public function deleteRejectsPutaway($event)
    {
        if ($this->businessType == $event->payload['business_type_id']) {
            $repository = BusinessType::getEloquentById($this->businessType);
            $repository->callback($event->payload, DeleteRejectsPutawayEvent::class);
        }
    }

    public function storeControlRecord($event)
    {
        $repository = BusinessType::getEloquentById($this->businessType);
        $repository->callback($event->payload, StoreControlRecordEvent::class);
    }

    public function updateControlRecord($event)
    {
        $repository = BusinessType::getEloquentById($this->businessType);
        $repository->callback($event->payload, UpdateControlRecordEvent::class);
    }

    public function deleteControlRecord($event)
    {
        $repository = BusinessType::getEloquentById($this->businessType);
        $repository->callback($event->payload, DeleteControlRecordEvent::class);
    }

    /**
     *
     * @param  \Illuminate\Events\Dispatcher  $events
     */
    public function subscribe($events)
    {
        $events->listen(
            StoreControlRecordEvent::class,
            static::class . '@storeControlRecord'
        );

        $events->listen(
            UpdateControlRecordEvent::class,
            static::class . '@updateControlRecord'
        );

        $events->listen(
            DeleteControlRecordEvent::class,
            static::class . '@deleteControlRecord'
        );
        $events->listen(
            StorePutawayEvent::class,
            static::class . '@storePutaway'
        );

        $events->listen(
            DeletePutawayEvent::class,
            static::class . '@deletePutaway'
        );

        $events->listen(
            StoreRejectsPutawayEvent::class,
            static::class . '@storeRejectsPutaway'
        );

        $events->listen(
            DeleteRejectsPutawayEvent::class,
            static::class . '@deleteRejectsPutaway'
        );
    }
}
